<template>
	<view>
		<view class="bg-white">
			<view class="top">
				<view class="title">积分商城</view>
			</view>
			
			<view class="score">
				<view class="left">
					<view class="num">{{$store.state.finance.score || 0}}</view>
					<view class="sub">我的积分</view>
				</view>
			
				<view class="right">
					<view class="btn" @click="$goto('/pages/mall/record')">
						<u-icon name="rmb-circle" style="margin-right: 8rpx;"></u-icon>
						<text>兑换记录</text>
					</view>
					<view class="sub">
						<text>如何赚取更多积分</text>
						<u-icon name="play-right-fill" size="20"></u-icon>
					</view>
				</view>
			</view>
			
			<view class="flex-row justify-center padding-tb-xl">
				<image src="../../static/images/mall-cj.png" style="width: 670rpx;height: 118rpx;" @click="$goto()"></image>
			</view>
		</view>

		<u-tabs :list="tabs" :is-scroll="false" inactive-color="#999999" active-color="#EEBC80" :current="current" @change="change"></u-tabs>

		<view class="goods-list">
			<my-mall-good v-for="i in list[current].data" :key="i.id" :good="i"></my-mall-good>
		</view>
		
		<view class="padding-tb">
			<u-loadmore :status="list[current].status" bg-color="#EFF1F4"></u-loadmore>
		</view>

		<my-need-login></my-need-login>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [{
					name: '全部商品'
				}, {
					name: '热门兑换'
				}, {
					name: '精品兑换'
				}, {
					name: '新品兑换'
				}],
				current: 0,
				list: [{
					type: 0,
					page: 1,
					data: [],
					status: 'loadmore'
				}, {
					type: 1,
					page: 1,
					data: [],
					status: 'loadmore'
				}, {
					type: 2,
					page: 1,
					data: [],
					status: 'loadmore'
				}, {
					type: 3,
					page: 1,
					data: [],
					status: 'loadmore'
				}]
			};
		},
		onLoad() {
			this.getUser()
			this.getList()
		},
		onPullDownRefresh() {
			this.getUser()
			this.getList(true);
		},
		methods: {
			change(index) {
				this.current = index;
				this.getList(true);
			},
			getUser() {
				this.$u.api.getUserinfo().then(res => {
					uni.stopPullDownRefresh();
				})
			},
			getList(clear = false) {
				let target = this.list[this.current]
				if (clear) {
					target.status = 'loadmore',
					target.page = 1,
					target.data = []
				}
				if (target.status == 'nomore') return;
				
				target.status = 'loading'
				this.$u.get('/mall/goods', {
					type: target.type,
					page: target.page
				}).then(res => {
					let { data, last_page } = res.data.result.goods
					target.data = [...target.data, ...data]
					if (target.page==last_page) target.status = 'nomore'
				})
			}
		}
	}
</script>

<style lang="scss">
	.top {
		width: 750rpx;
		height: 400rpx;
		background: url(../../static/images/mall-bg.png);
		background-size: 100% 100%;
		display: flex;
		justify-content: center;
		align-items: center;

		.title {
			margin-top: -180rpx;
			width: 160rpx;
			height: 56rpx;
			font-size: 40rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: rgba(255, 255, 255, 1);
			line-height: 56rpx;
		}
	}

	.score {
		width: 670rpx;
		height: 218rpx;
		background: rgba(255, 255, 255, 1);
		box-shadow: 0rpx 4rpx 40rpx 0rpx rgba(0, 0, 0, 0.1);
		border-radius: 16rpx;
		margin: 0 auto;
		margin-top: -180rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 48rpx;

		.left,
		.right {
			height: 50%;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
		}

		.num {
			font-size: 64rpx;
			font-family: Helvetica-Bold, Helvetica;
			font-weight: bold;
			color: rgba(102, 169, 252, 1);
		}

		.btn {
			width: 192rpx;
			height: 60rpx;
			background: linear-gradient(270deg, rgba(103, 212, 219, 1) 0%, rgba(102, 166, 255, 1) 100%);
			border-radius: 29rpx;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: rgba(255, 255, 255, 1);
			line-height: 60rpx;
			text-align: center;
		}

		.sub {
			font-size: 24rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: rgba(153, 153, 153, 1);
		}
	}

	.goods-list {
		display: flex;
		flex-flow: row wrap;
		justify-content: space-between;
		padding: 40rpx;
		padding-top: 20rpx;
	}
</style>
